package com.example.admin.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 王同学
 */
@Component
@Slf4j
public class TokenIntercepeor implements HandlerInterceptor {
    //这边的用法——前置后置 放行
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //如果有拦截的话，就会执行这边的代码
        log.info("拦截了一手");
        log.info(request.getRequestURI());
        //打路径日志
        //这边添加放行条件，如果请求头满足则返回true放行，这边写find 请求头，包含了findAll和findById两种请求
        if (request.getRequestURI().startsWith("/blog/find")||request.getRequestURI().startsWith("/category/find")){
            return true;
        }
        return false;
    }

    @Override    //这个方法目前不怎么用了
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
